package pri.hillchen.springbootstd.sso.common.response;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author:hillchen
 * @DESCRIPTION:
 * @Date: Created in 9:17 2017/12/26 0026.
 */
public class ListResponse<T> extends BaseResponse<List<T>> implements RecodesResponse<T>  {
    private static ListResponse EMPTY_LIST_RESPONSE = new ListResponse();
    @Override
    public boolean isEmpty() {
        return CollectionUtils.isEmpty(getReqResult());
    }

    @Override
    public void addRecode(T recode) {
        List<T> recodes = getReqResult();
        if(recodes == null){
            recodes = new ArrayList<T>();
            setReqResult(recodes);
        }
        recodes.add(recode);
    }

    @Override
    public void addRecodes(Collection<T> recodes) {
        if(!CollectionUtils.isEmpty(recodes)){
            List<T> reqResult = getReqResult();
            if(reqResult == null){
                reqResult = new ArrayList<T>();
                setReqResult(reqResult);
            }
            reqResult.addAll(recodes);
        }
    }

    public static <T> ListResponse<T> createEmptyResponse(){
        return EMPTY_LIST_RESPONSE;
    }
}
